package com.zja.www.controller;


import com.zja.www.service.DoctorService;
import com.zja.www.service.MedicalRecordService;
import com.zja.www.service.UserService;

import java.util.InputMismatchException;
import java.util.Scanner;
/**
 * @author ja
 * @create 2025/3/30
 * @description Basecontroller控制层
 */
public class BaseController {

    protected static final Scanner scanner = new Scanner(System.in);


        protected final UserService userService;
        protected final DoctorService doctorService;
        protected final  MedicalRecordService medicalRecordService;
        // 通过构造函数注入
        public BaseController(UserService userService,
                              DoctorService doctorService,
                              MedicalRecordService medicalRecordService) {
            this.userService = userService;
            this.doctorService = doctorService;
            this.medicalRecordService = medicalRecordService;
        }

    /**
     * 安全读取整数输入
     * @return 用户输入的整数
     * 循环直到用户输入有效数字
     */
    public int readIntInput() {
        while (true) {
            try {
                int num = scanner.nextInt();
                scanner.nextLine(); // 清除缓冲区中的换行符
                return num;
            } catch (InputMismatchException e) {
                System.out.print("输入无效，请重新输入数字: ");
                scanner.nextLine(); // 清除错误的输入
            }
        }
    }

    /**
     * 安全读取字符串输入
     * @return 去除首尾空格的输入字符串
     * 自动trim()避免空白字符问题
     */
    public String readStringInput() {
        return scanner.nextLine().trim();  // 读取并清理输入
    }



    /**
     * 密码二次验证
     * @return 验证通过后的密码字符串
     * 要求用户重复输入两次密码
     * 两次输入不一致时循环提示
     */
    protected String readPasswordWithConfirm() {
        String password, confirmPassword;
        do {
            System.out.print("请输入密码: ");
            password = readStringInput();           // 首次输入
            System.out.print("请再次输入密码: ");
            confirmPassword = readStringInput();    // 确认输入

            if (!password.equals(confirmPassword)) {
                System.out.println("两次密码不一致！");  // 验证提示
            }
        } while (!password.equals(confirmPassword)); // 循环直到一致

        return password;  // 返回确认后的密码
    }
}
